package com.fastmobile.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.json.JSONException;
import org.json.JSONObject;

import com.fastmobile.dao.ContactsListDao;
import com.fastmobile.entity.ContactsList;
import com.fastmobile.entity.Departments;
import com.fastmobile.entity.Transactions;
import com.fastmobile.entity.YourCompany;

public class ContactsListImpl implements ContactsListDao{
	
	protected EntityManager em;
	
	public EntityManager getEntityManager() {
		return em;
	}
	
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ContactsList> getAllContactsList() {
		Query query = em.createNativeQuery("SELECT * FROM CONTACTSLIST c", ContactsList.class);
		return query.getResultList();
	}
	
	@Override
	public ContactsList getContactsListByUserNameAndPassWord(String userName, String passWord) {
		String script = "SELECT * FROM CONTACTSLIST c WHERE c.userName = :userName and c.password = :passWord ";
		Query query = em.createNativeQuery(script, ContactsList.class);
		query.setParameter("userName", userName);
		query.setParameter("passWord", passWord);
		return (ContactsList) query.getResultList().get(0);
	}
	
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<JSONObject> getJsonContactsList() throws JSONException {
//		List<ContactsList> list = getAllContactsList();
//		List<JSONObject> listObj = new ArrayList<JSONObject>();
//		for (ContactsList contact:list) {
//			Departments depart = getDepartmentsByDeptId(contact.getDeptId());
//			YourCompany company = getYourCompanyByCmdId(depart.getCmpId());
//			if (company.getActive() == true) {
//				JSONObject json = new JSONObject();
//				json.put("contactId", contact.getContactId());
//				json.put("userName", contact.getUserName());
//				listObj.add(json);
//			}
//		}
//		return listObj;
//	}
	
//	@SuppressWarnings("unchecked")
//	@Override
//	public JSONObject getJsonContactByUserNameAndPassWord(String userName, String passWord) throws JSONException {
//		ContactsList contact = getContactsListByUserNameAndPassWord(userName, passWord);
//		Departments department = getDepartmentsByDeptId(contact.getDeptId());
//		YourCompany yourCompany = getYourCompanyByCmdId(department.getCmpId());
//		JSONObject json = new JSONObject();
//		json.put("userName", contact.getUserName());
//		json.put("deptId", contact.getDeptId());
//		json.put("accessRight", contact.getAccessRight());
//		json.put("yourCompany", yourCompany.getCmpId());
//		return json;
//	}
	
	@Override
	public Departments getDepartmentsByDeptId(String deptId) {
		return getEntityManager().find(Departments.class, deptId);
	}
	
	@Override
	public YourCompany getYourCompanyByCmdId(String cmdId) {
		return getEntityManager().find(YourCompany.class, cmdId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ContactsList> getContacsListByDeptId(String deptId) {
		Query query = em.createNativeQuery("SELECT * FROM CONTACTSLIST c where c.deptId = :deptId", ContactsList.class);
		query.setParameter("deptId", deptId);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<JSONObject> getJsonContactsListByDeptId(String deptId) throws JSONException {
		List<ContactsList> list = getContacsListByDeptId(deptId);
		List<JSONObject> listObj = new ArrayList<JSONObject>();
		for (ContactsList contact:list) {
			JSONObject json = new JSONObject();
			json.put("contactId", contact.getContactId());
			json.put("userName", contact.getUserName());
			listObj.add(json);
		}
		return listObj;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getDetail(String deptId) {
		String queryString = "SELECT contact.*, departments.* FROM CONTACTSLIST contact "+
						"INNER JOIN DEPARTMENTS department on contact.deptId = departments.deptId "+
						"WHERE contact.deptId = :deptId";
		Query query = em.createNativeQuery(queryString);
		query.setParameter("deptId", deptId);
		return query.getResultList();
	}

	@Override
	public List<JSONObject> getJsonContactsList() throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject getJsonContactByUserNameAndPassWord(String userName,
			String passWord) throws JSONException {
		// TODO Auto-generated method stub
		return null;
	}
}
